package main;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author Yan Couto
 */
public class OptionsFrame extends JFrame{

    Box b;
    JPanel p;
    Eventer evt;
    JButton ball,shot,back;
    Color bc,sc;
    int isBall;

    public OptionsFrame () {
        initialize();
    }

    private void initialize() {
       this.setSize(500,500);
       this.setResizable(false);
       this.setTitle("Options");
//       this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
       this.addWindowListener(evt);

       bc = Color.RED;
       sc = Color.BLACK;
       evt = new Eventer();

       b = Box.createVerticalBox();
       p = new JPanel();
       ball = new JButton("Set Ball Color");
       ball.addActionListener(evt);
       shot = new JButton("Set Shots Color");
       shot.addActionListener(evt);
       back = new JButton("Back");
       back.addActionListener(evt);

       b.add(Box.createVerticalGlue());
       b.add(ball);
       b.add(Box.createVerticalStrut(5));
       b.add(shot);
       b.add(Box.createVerticalStrut(8));
       b.add(back);
       b.add(Box.createVerticalGlue());

       p.add(b,BorderLayout.CENTER);
       this.add(p);
       this.pack();
       this.setLocationRelativeTo(BallGame.BG.wf);
       this.setVisible(true);
    }

    private void setColor () {
        if(isBall==1)
           bc = JColorChooser.showDialog(this,"Choose the Ball color:", Color.RED);
       else if(isBall==-1)
           sc = JColorChooser.showDialog(this,"Choose the Shots color:", Color.BLACK);
        BallGame.ballColor =bc;
        BallGame.shotColor =sc;
    }



    private class Eventer extends WindowAdapter implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if(e.getSource()==ball) {
                isBall=1;
                setColor();
            }
            else if(e.getSource()==shot){
                isBall=-1;
                setColor();
            }
            else if(e.getSource()==back) {
                setVisible(false);
            }

        }

        @Override
        public void windowClosing(WindowEvent e) {
            back.doClick();
        }

    }
}